package com.topway.dsmflex.ui.composite.synchro;

import java.util.Date;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

import com.topway.dsmflex.model.synchro.DataSqlBean;
import com.topway.dsmflex.ui.InitFormComposite;
import com.topway.dsmflex.util.StringUtil;

/**
 * 系统日志
 * 
 * @author liusy 2013-04-28
 *
 */
public class SystemLog extends InitFormComposite{
	
   public static String name = "系统日志";
   
   private static Logger log = Logger.getLogger(SystemLog.class);
   
   private static StyledText  txtOutput;

   public static final int ERROR = 0;
   
   public static final int WARNINGS = 1;
   
   public static final int PROMPT = 2;

   /**
    * Create the composite
    * 
    * @param parent
    * @param style
    */
   public SystemLog(Composite parent, int style) {
	   super(parent, style);
	   createContents();
      initForm();
   }

   public int initForm() {
	   return 1;
	  // Display.getDefault().asyncExec();
	  // new PrintLog(txtOutput).start();
   }
   
   protected void createContents() {
      setLayout(new FillLayout());
      txtOutput = new StyledText(this, SWT.BORDER|SWT.V_SCROLL);
		final Menu menu = new Menu(this.getShell(), SWT.POP_UP);
		final MenuItem menuAdd = new MenuItem(menu, SWT.PUSH);// 开始
		menuAdd.setText("清除");
		menuAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				clearLog();
			}
		});
                                                               
     txtOutput.setMenu(menu);
      txtOutput.setEditable(false);
   }
   
   
   public static void clearLog()
   {
       Display.getDefault().asyncExec(new Thread() {
           public void run() {
        	   txtOutput.setText("");
           }
        });
	 
   }
   public static void writeLog(int type,final String loginfo){
	   if (type == 0) {
		   log.error(loginfo);
	         Display.getDefault().asyncExec(new Thread() {
	            public void run() {
	            	final int start = txtOutput.getText().length() + 1;
	                final int length = loginfo.length();
	                txtOutput.append("\n"+StringUtil.dateToString(new Date())+"：" + loginfo);
	               Color color = txtOutput.getShell().getDisplay().getSystemColor(SWT.COLOR_RED);
	               StyleRange range = new StyleRange(start, length, color, null);
	               txtOutput.setStyleRange(range);
	               txtOutput.setSelection(txtOutput.getCharCount());
	            }
	         });
	      }
	   else if (type == 0) {
		   log.info(loginfo);
	         Display.getDefault().asyncExec(new Thread() {
		            public void run() {
		            	final int start = txtOutput.getText().length() + 1;
		                final int length = loginfo.length();
		                txtOutput.append("\n"+StringUtil.dateToString(new Date())+"：" + loginfo);
		               Color color = txtOutput.getShell().getDisplay().getSystemColor(SWT.COLOR_DARK_YELLOW);
		               StyleRange range = new StyleRange(start, length, color, null);
		               txtOutput.setStyleRange(range);
		               txtOutput.setSelection(txtOutput.getCharCount());
		            }
		         });
		      }
	   else
	   {
		   log.info(loginfo);
	   Display.getDefault().asyncExec(new Thread() {
           public void run() {
        	   txtOutput.append("\n" +StringUtil.dateToString(new Date())+"："+ loginfo);
        	   txtOutput.setSelection(txtOutput.getCharCount());
           }
        });
	   }
   }
   
   
  
}
